package com.vcc.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.RedisCustomizeUtils;
import com.vcc.data.entity.BossRolePermission;
import com.vcc.data.entity.BossUserPermission;
import com.vcc.data.mapper.BossUserPermissionMapper;
import com.vcc.service.service.BossUserPermissionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class BossUserPermissionServiceImpl extends ServiceImpl<BossUserPermissionMapper, BossUserPermission> implements BossUserPermissionService {
    @Override
    public List<BossUserPermission> listByUserId(Long userId) {
        String key = "user_user_permission:" + userId;
        if (RedisCustomizeUtils.hasKey(key)) {
            return RedisCustomizeUtils.getList(key, BossUserPermission.class);
        } else {
            List<BossUserPermission> list = queryChain().eq(BossUserPermission::getBossUserId, userId).list();
            if (list != null && !list.isEmpty()) {
                RedisCustomizeUtils.set(key, list, 5L, TimeUnit.MINUTES);
            }
            return list;
        }
    }

    @Override
    public Boolean removeByUserId(Long userId) {
        return remove(queryChain().eq(BossUserPermission::getBossUserId, userId));
    }

    @Override
    public Boolean removeByUserIdAndPermissionId(Long userId, Long permissionId) {
        return remove(queryChain().eq(BossUserPermission::getBossUserId, userId)
                .eq(BossUserPermission::getPermissionId, permissionId));
    }

    @Override
    public Boolean existsByUserIdAndPermissionId(Long userId, Long permissionId) {
        return queryChain().eq(BossUserPermission::getBossUserId, userId)
                .eq(BossUserPermission::getPermissionId, permissionId).exists();
    }
}
